<template>
  <page-container>
    <search-form
      v-model="searchForm"
      :searchColumns="searchColumns"
      @on-search="handleSearch">
    </search-form>

    <avue-table
      :page="page"
      :loading="loading"
      :dataList="dataList"
      :tableColumns="tableColumns"
      @onPageChange="onPageChange">
    </avue-table>
  </page-container>
</template>

<script>
import tableMixins from '@/mixins/table'
import { getLogList, getQuerySearch } from '@/api/permission'
import { searchColumns, tableColumns } from './composes/table-config'
import { mapGetters } from 'vuex'

export default {
  mixins: [tableMixins],
  data() {
    return {
      dataList: [],
      searchForm: {
        daterange: [this.$startStamp(new Date()), this.$endStamp(new Date())],
      },
      userArray: [],
    }
  },
  computed: {
    tableColumns,
    searchColumns,
    ...mapGetters(['userList', 'addRoutes']),
    routerMap() {
      const routerMap = {}
      const generateRouterMap = routerList => {
        routerList.forEach(item => {
          routerMap[item.name] = item
          if (item?.children?.length) {
            generateRouterMap(item.children)
          }
        })
      }
      generateRouterMap(this.addRoutes)
      return routerMap
    },
    getAllResourceList() {
      const modelList = []
      this.addRoutes.forEach(item => {
        const chilArr = item?.children.map(chilItem => {
          return {
            ...chilItem,
            Title: `${this.$t('routes.' + item?.meta?.title)}/${this.$t(
              'routes.' + chilItem?.meta?.title,
            )}`,
          }
        })
        modelList.push(...chilArr)
      })
      return modelList
    },
  },
  methods: {
    // 注册定义查询方法，用于tableMixins(必须，不然就自己写查询逻辑, 具体可以查看mixins/table.js)
    // fetchMethod: getLogList,
    // 查询封装
    async getList() {
      this.dataList = []

      const searchParams = this.formatSearch()
      const queryParams = {
        ...searchParams,
        searchPage: this.page.currentPage,
        searchPageSize: this.page.pageSize,
      }

      this.loading = true

      const { data } = await getLogList(queryParams)

      this.dataList = data?.List ?? []
      this.page.total = data?.TotalCnt ?? 0
      this.summaryData = data ?? {}

      this.loading = false
    },
    async getQuerySearch() {
      const { data } = await getQuerySearch()
      this.userArray = data?.UserList
    },

    // 格式化查询参数的
    formatSearch() {
      const searchParams = { ...this.searchForm }
      const [start, end] = this.searchForm.daterange ?? []
      searchParams.start = start && this.$dayjs(this.$startStamp(start)).unix()
      searchParams.end = end && this.$dayjs(this.$endStamp(end)).unix()
      delete searchParams.daterange
      return searchParams
    },
  },
  mounted() {
    this.getQuerySearch()
    this.getList()
  },
}
</script>
